﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MinimalisticTelnet;
using System.IO;

namespace UFSControl
{
    public partial class deviceTelnetInfo : Form
    {
        private string IP;

        public deviceTelnetInfo()
        {
            InitializeComponent();
            IniFile lIni = new IniFile();
            IP = lIni.Read("IP", "Receiver");
            ReadDeviceInfo();
            
        }

        //über Telnet Daten auslesen
        private void ReadDeviceInfo()
        {
            //create a new telnet connection to hostname on port "23"
            TelnetConnection tc = new TelnetConnection(IP, 23);

            //login with user "",password "", using a timeout of 100ms, and show server output
            string s = tc.LoginWithoutUserPwd(500);
            //Console.Write(s);

            // server output should end with "$" or ">", otherwise the connection failed
            string prompt = s.TrimEnd();
            prompt = s.Substring(prompt.Length - 1, 1);
            if (prompt != "$" && prompt != ">" && prompt != "#")
                throw new Exception("Connection failed");
            //Befehl absetzen
            tc.WriteLine("cat /proc/cpuinfo");
            System.Threading.Thread.Sleep(500);
            //Console auslesen
            string input = tc.Read();
            StringReader lReader = new StringReader(input);
            string aLine = lReader.ReadLine();
            //echo überlesen
            aLine = lReader.ReadLine();
            aLine = aLine.TrimEnd();
            while (aLine != null){
                aLine = aLine.TrimEnd();
                if (aLine.Substring(aLine.Length - 1, 1) == "#") break; // wenn Ende gefunden, dann gleich Schleife verlassen
                if (aLine.Contains(':'))
                {
                    string[] split = aLine.Split(':');
                    listView.Items.Add(new ListViewItem(new string[] { split[0], split[1] }));
                }
                else
                {
                    listView.Items.Add(new ListViewItem(new string[] { aLine, "" }));
                }
                aLine = lReader.ReadLine();
            }
            //Befehl absetzen
            tc.WriteLine("cat /proc/version");
            System.Threading.Thread.Sleep(500);
            //Console auslesen
            input = tc.Read();
            lReader = new StringReader(input);
            aLine = lReader.ReadLine();
            //echo überlesen
            aLine = lReader.ReadLine();
            listView.Items.Add(new ListViewItem(new string[] { "version", aLine }));
            //beenden
            tc.WriteLine("exit");
        }

        //Form schließen
        private void okButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
